package org.glow.changgou.goods.congroller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.glow.changgou.entity.Result;
import org.glow.changgou.entity.StatusCode;
import org.glow.changgou.goods.pojo.Brand;
import org.glow.changgou.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-09-20 13:11:48
 * @Description
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result<Brand> findAll()
    {
        return new Result<Brand>(true, StatusCode.OK,"success",brandService.findAll());
    }
    @GetMapping("{id}")
    public Result<Brand> findById(@PathVariable Integer id)
    {
        return new Result<Brand>(true,StatusCode.OK,"successFindId",brandService.findById(id));
    }
    @PostMapping()
    public Result add(@RequestBody Brand brand)
    {
        brandService.addNewBrand(brand);
        return new Result(true,StatusCode.OK,"添加品牌成功！");
    }
    @PutMapping("{id}")
    public Result updateBrandById(@RequestBody Brand brand, @PathVariable Integer id)
    {
        brandService.updateBrandById(brand,id);
        return new Result(true,StatusCode.OK,"修改品牌成功！");
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id)
    {
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除品牌成功！");
    }
    @PostMapping("search")
    public Result<List<Brand>> findlist(@RequestBody(required = false)Brand brand)
    {
        return new Result<List<Brand>>(true,StatusCode.OK,"查询成功",brandService.findList(brand));
    }
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //分页查询
        return new Result<Page>(true,StatusCode.OK,"查询成功",brandService.findPage(page,size));
    }
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) Brand brand, @PathVariable  int page, @PathVariable  int size){
        //执行搜索
        Page<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }
    /***
     * 根据分类实现品牌列表查询
     * /brand/category/{id}  分类ID
     */
    @GetMapping(value = "/category/{id}")
    public Result<List<Brand>> findBrandByCategory(@PathVariable(value = "id")Integer categoryId){
        //调用Service查询品牌数据
        List<Brand> categoryList = brandService.findByCategory(categoryId);
        return new Result<List<Brand>>(true,StatusCode.OK,"查询成功！",categoryList);
    }
}
